package com.neusoft.neuiotms.dcam.fixed.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.AhAssetCardDTO;
import com.neusoft.neuiotms.dcam.fixed.service.AhAssetCardService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "资产卡片管理")
@RestController
@RequestMapping(value = "/api/w/fixed/ahassetcards")
public class AhAssetCardController {

    @Autowired
    private AhAssetCardService ahAssetCardService;

    @ApiOperation("查询所有资产卡片")
    @GetMapping
    public ResultListDTO<AhAssetCardDTO> index(Searchable searchable) {
        final List<AhAssetCardDTO> list = this.ahAssetCardService.search(searchable);
        return ResultListDTO.success(list);
    }

    @ApiOperation("分页查询所有资产卡片")
    @GetMapping("/p")
    public PageResultDTO<AhAssetCardDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.ahAssetCardService.search(pageable, searchable));
    }

    @ApiOperation("创建资产卡片")
    @PostMapping
    public ResultDTO<AhAssetCardDTO> create(@RequestBody final AhAssetCardDTO dto) {
        final AhAssetCardDTO result = this.ahAssetCardService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("资产卡片详情")
    @GetMapping("/{id}")
    public ResultDTO<AhAssetCardDTO> details(@PathVariable final Long id) {
        final AhAssetCardDTO dto = this.ahAssetCardService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("修改资产卡片")
    @PutMapping("/{id}")
    public ResultDTO<AhAssetCardDTO> update(@PathVariable final Long id,
            @RequestBody final AhAssetCardDTO dto) {
        final AhAssetCardDTO result = this.ahAssetCardService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("修改资产卡片关联资产")
    @PutMapping("/{id}/associate")
    public ResultDTO<?> associate(@PathVariable final Long id,
            @RequestBody final List<Long> assets) {
        this.ahAssetCardService.associate(id, assets);
        return ResultDTO.success();
    }

    @ApiOperation("导出资产卡片")
    @GetMapping(value = "/export")
    public void download(final Searchable searchable,
            final HttpServletResponse response) {

        final Workbook workbook = this.ahAssetCardService.export(searchable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("资产卡片导出" + LocalDateTime.now() + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            AhAssetCardController.log.error("资产卡片导出失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AhAssetCardController.log.error("资产卡片导出关闭流失败", e);
            }
        }

    }

    @ApiOperation("删除资产卡片")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.ahAssetCardService.delete(id);
        return ResultDTO.success();
    }
}
